﻿
function init() {
    feedbackdict();
    $('#save').bind('click', function () { save(); });
}

function feedbackdict() {
    $.ajaxHandler({
        url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        urlType: "QueryDictItemList2",
        model: function () { return Sys_Config.FeedbackDict; },
        success: function (data) {
            if (data && data.length > 0) {
                var _html = [];
                $.each(data, function (i, item) {
                    if (lang.SysLanguage == "en") {
                        _html.push('<option value="' + item.ITEMVALUE + '">' + item.ITEMDESC + '</option>');
                    }
                    else {
                        _html.push('<option value="' + item.ITEMVALUE + '">' + item.ITEMNAME + '</option>');
                    }
                });
                $('#feedbacktype').append(_html.join(''));
            }
        }
    });
}

//上传
function doUpload(idstr, namestr, attType, formid) {
    var file = $('#upload' + idstr).val();
    if ($.trim(file) == '') {
        alert(lang.select_need_to_upload + namestr);
        return false;
    }
    var ext = file.substr(file.lastIndexOf('.') + 1);
    var extReg = /^(jpg|gif|png|doc|docx|xls|xlsx|ppt|pptx|rar|zip|pdf|txt)$/;
    if (!extReg.test(ext)) {
        alert(lang.the_following_types_only_support + '：jpg、gif、png、txt、doc、docx、xls、xlsx、ppt、pptx、pdf、zip、rar');
        return false;
    }
    var options = {
        type: "post",
        dataType: 'json',
        url: App_Config.TadongUrl + '/Upload.aspx?fileType=feedback',
        success: function (data) {
            if (data.status == 0) {
                if (file.lastIndexOf('\\') > 0) {
                    file = file.substr(file.lastIndexOf('\\') + 1); //chrome会变成c;/fakepath/xxx.jpg
                }
                var _url = downloadurl('feedback', data.fileName, data.realName);
                $('#attachinfo').html('<span><input id="attach_name" type="hidden" value="' + data.realName + '"/><input id="attach_url" type="hidden" value="' + data.fileName + '"/><a href = "' + _url + '">' + data.realName + '</a><span onclick="deleteattach();" style="cursor:pointer;padding-left:15px;color:blue;">' + lang.delete_ + '</span></span>');
            } 
            else {
                alert(lang.upload_failure);
            }
        }
    };
    $('#' + formid).ajaxSubmit(options);
}

function deleteattach() {
    $('#attachinfo').html('');
}


//保存
function save() {
    if ($('#title').val() == '') {
        alert(lang.title_can_not_be_empty);
        return;
    }

    $.ajaxHandler({
        url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        urlType: "FeedbackAdd",
        model: function () {
            var obj = {};
            obj.FEEDBACK_TYPE = $('#feedbacktype').val();
            obj.FEEDBACK_TITLE = $('#title').val();
            obj.FEEDBACK_CONTENT = $('#content').val();
            if ($('#attach_name').val() != '') {
                obj.FEEDBACK_ATTACHNAME = $('#attach_name').val();
                obj.FEEDBACK_ATTACHURL = $('#attach_url').val();
                obj.FEEDBACK_ATTACHTYPE = "feedback";
            }

            return $.toJSON(obj);
        },
        success: function (data) {
            if (data) {
                alert(lang.thank_you_for_your_feedback);
                window.location.reload();
            }
        }
    });
}